<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@include file="../head.jsp"%>
<html>
<head>
    <script type="text/javascript">
        (function(){
            Ext.onReady(function(){
                Ext.QuickTips.init();
                var id = '';

                Ext.define('Role', {
                    extend: 'Ext.data.Model',
                    fields: [
                        {name:'id',type:'string'},
                        {name:'name',type:'string'},
                        {name:'roleDesc',type:'string'},
                        {name:'status',type:'string'},
                        {name:'version',type:'string'}
                    ]
                });

                Ext.define('SysOperator', {
                    extend: 'Ext.data.Model',
                    fields: [
                        {name:'id',type:'string'},
                        {name:'name',type:'string'},
                        {name:'sex',type:'string'},
                        {name:'phone',type:'string'},
                        {name:'qq',type:'string'},
                        {name:'mail',type:'string'},
                        {name:'departmentId',type:'string'},
                        {name:'departmentName',type:'string'},
                        {name:'idCard',type:'string'},
                        {name:'version',type:'int'},
                        {name:'status',type:'int'}
                    ]
                });

                var name = Ext.create('Ext.form.field.Text',{
                    labelWidth:55,
                    id:'queryName',
                    name:'queryName'
                });


                var department = Ext.create('Ext.form.ComboBox',{
                    queryMode: 'local',
                    displayField: 'label',
                    valueField: 'value',
                    store:Ext.create('Ext.data.Store', {
                        model: 'commonModel',
                        proxy: {
                            type:'ajax',
                            url:'/fsxt/sysBase!getDepartment.action'
                        },
                        autoLoad: true,
                        listeners:{
                            load:function(){
                                var insert = Ext.create('commonModel',{value:'',label:'0-全部'});
                                this.insert(0,insert);
                            }
                        }
                    })
                });

                var operatorGrid = Ext.create('Ext.grid.Panel', {
                    title: '人员列表',
                    autoScroll:true,
                    loadMask: true,
                    columns:[
                        {text: "人员名称",dataIndex:'name',sortable: true},
                        {text: "电话",dataIndex:'phone',sortable: true},
                        {text: "邮箱",dataIndex:'mail',sortable: true},
                        {text: "所属部门",dataIndex:'departmentName',sortable: true}
                    ],
                    tbar:Ext.create('Ext.toolbar.Toolbar',{
                        items:['-',{
                            xtype:'label',
                            text:'名称'
                        },name,'-',{
                            xtype:'label',
                            text:'部门'
                        },department,'-',{
                            xtype:'button',
                            iconCls:'find',
                            text:'查询',
                            handler:function(){
                                operatorGrid.store.load();
                            }
                        },'-']
                    }),
                    store:Ext.create('Ext.data.Store',{
                        model: 'SysOperator',
                        proxy: {
                            type:'ajax',
                            url:'/fsxt/sys/sysOperator!getSysOperator.action',
                            reader: {
                                root: 'root',
                                totalProperty: 'totalProperty'
                            }
                        },
                        autoLoad: true,
                        listeners:{
                            beforeload:function(){
                                this.proxy.extraParams = {
                                    name:encodeURIComponent(name.getValue()),
                                    departmentId:department.getValue(),
                                    status:1
                                }
                            }
                        }
                    }),
                    listeners:{
                        itemclick:function(view,record,item,index,event,options){
                            id = record.data.id;
                            requestRole(record.data.id);
                        }
                    }
                });

                var sm = Ext.create('Ext.selection.CheckboxModel');

                var roleGrid = Ext.create('Ext.grid.Panel',{
                    title: '角色列表',
                    selModel:sm,
                    loadMask: true,
                    autoScroll:true,
                    columns:[
                        Ext.create('Ext.grid.RowNumberer'),
                        {text: "角色名称",dataIndex: 'name',sortable: true},
                        {text: "角色描述",width:250,dataIndex: 'roleDesc',sortable: true}
                    ],
                    tbar:Ext.create('Ext.toolbar.Toolbar',{
                        items:['-',{
                            xtype:'button',
                            iconCls:'accept',
                            text:'保存',
                            handler:function(){
                                if(SysBase.isBlank(id)){
                                    Ext.Msg.show({title:'提示',msg:'未能获取人员',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                                    return;
                                }
                                var ids = '';
                                var selected = roleGrid.getView().getSelectionModel().getSelection();
                                for(var i = 0 ; i < selected.length ; i ++){
                                    var record = selected[i];
                                    ids = ids + record.data.id + ',';
                                }
                                var params = {};
                                params.id = id;
                                params.ids = ids;
                                var data = {};
                                data.params = params;
                                data.url = '/fsxt/sys/roleOperator!saveOperatorRole.action';
                                callAjax.call(data);
                            }
                        },'-']
                    }),
                    store:Ext.create('Ext.data.Store',{
                        model: 'Role',
                        proxy: {
                            type:'ajax',
                            url:'/fsxt/sys/role!getRole.action',
                            reader: {
                                root: 'root',
                                totalProperty: 'totalProperty'
                            }
                        },
                        autoLoad: true,
                        listeners:{
                            beforeload:function(){
                                this.proxy.extraParams = {
                                    status:1
                                }
                            }
                        }
                    })
                });

                function requestRole(id){
                    Ext.MessageBox.wait("", "正在请求,请稍候...");
                    Ext.Ajax.request({
                        url:'/fsxt/sys/roleOperator!getRole.action',
                        method: 'post',
                        params: {id:id},
                        success:function(response,options){
                            Ext.MessageBox.hide();
                            var ids = response.responseText.split(',');
                            var records = [];
                            roleGrid.store.each(function(record){
                                for(var i = 0 ; i < ids.length ; i ++){
                                    if(record.data.id == ids[i]){
                                        records.push(record);
                                    }
                                }
                            });
                            roleGrid.getSelectionModel().select(records);
                        },
                        failure:function(response,options){
                            Ext.MessageBox.hide();
                            Ext.Msg.show({title:'提示',msg:'获取此用户对应角色失败!.',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.ERROR});
                        }
                    });
                }

                new Ext.Viewport({
                    border:false,
                    layout:'border',
                    items:[{
                        width:500,
                        region:'west',
                        split:true,
                        items: [operatorGrid]
                    },{
                        region:'center',
                        items: [roleGrid]
                    }]
                });

            });
        })();
    </script>
</head>
<body>
</body>
</html>